#!/bin/bash
. ./public_functions #公共函数库 

##1 检查管理机系统版本
check_Version_local() {

  system_version_centos7=`rpm -q centos-release | cut -d- -f3`
  system_version_rocky9=`rpm -q rocky-release | cut -d- -f3 | cut -d. -f1`

  if [[ $system_version_centos7 -ne 7 && $system_version_rocky9 -ne 9 ]]; then
      logR "Sorry~ 仅支持Centos7.x 或者Rocky9.x 的系统使用"
      exit 1
  fi
}
check_Version_local && clear ##1

##2 生成本机公钥和私钥
my_Ssh_keygen() {

  logW "\n1. 开始生成本机私钥和公钥" #1-20#
  logY "ssh-keygen -P '' -f ~/.ssh/id_rsa" #1-20#
    ssh-keygen -P '' -f ~/.ssh/id_rsa
  if [[ $? -eq 0 ]]; then
    logG "success ↑↑↑" && echo
  
  fi
}

##3 输入并验证 用户名 ip地址 端口号
input_User_ip_port() {

  clear
  echo -n "请输入目标主机 用户名: "
  read sshuser
  echo -n "请输入目标主机 ip地址: "
  read sship
  echo -n "请输入目标主机 端口号: "
  read sshport
  
  ## 对输入的参数进行检查
  echo $sship | grep -qE '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'
  if [[ $? -ne 0 ]]; then
    logR "ip地址不合法, 请重新输入!" && sleep 2 #1-20#
    input_User_ip_port ##2
  fi
  
  echo $sshport | grep -qE '^[0-9]{1,6}$'
  if [[ $? -ne 0 ]]; then
    logR "端口号不合法，请重新输入!" && sleep 2 #1-20#
    input_User_ip_port ##2
  fi
}

##4 发送公钥到远程主机
my_Ssh_copy_id() {

  input_User_ip_port ##3
  logW "\n1. 开始发送公钥到远程主机" #1-20#
  logY "ssh-copy-id -p $sshport $sshuser@$sship" #1-20#
    ssh-copy-id -p $sshport $sshuser@$sship
  checkTF #21#

  systemtype=$(ssh -p $sshport $sshuser@$sship 'uname -a | grep -Eo ubuntu')
  if [[ $systemtype == ubuntu ]]; then
     echo "目标主机为ubuntu，请输入用户$sshuser的密码(使执行sudo免密): "
     read -s sshpasswd
     ssh -p $sshport $sshuser@$sship "echo "$sshpasswd" | sudo -S sed -i '\$a ubuntu ALL=(ALL) NOPASSWD: ALL' /etc/sudoers &>/dev/null"
     echo
     ssh -p $sshport $sshuser@$sship "sudo tail -1 /etc/sudoers"
     echo
  fi
}

##5 安装nginx到远程主机 (可变模块)
nginx_Install(){

  logW "\n--------软件为在线下载------------------开始部署NGINX-----------------------------------"

  name=nginx
  logW "\n1. 传送脚本到远程主机" #1-20#
  logY "scp -P $sshport $name-install.sh public_functions $sshuser@$sship:/tmp/" #1-20#
    scp -P $sshport $name-install.sh public_functions $sshuser@$sship:/tmp/
  checkTF #21#
  
  logW "2. 修改远程脚本文件为可执行" #1-20#
  logY "ssh -p $sshport $sshuser@$sship 'chmod +x /tmp/$name-install.sh /tmp/public_functions'" #1-20#
    ssh -p $sshport $sshuser@$sship 'chmod +x /tmp/'$name'-install.sh /tmp/public_functions'
  checkTF #21#
  
  logW "3. 远程执行脚本文件" #1-20#
  logY "ssh -p $sshport $sshuser@$sship '/tmp/$name-install.sh'" #1-20#
    logG "---------------------远程主机开始安装---------------------\n"
    ssh -p $sshport $sshuser@$sship 'bash /tmp/'$name'-install.sh'
}

##6 安装jdk到远程主机 (可变模块)
jdk_Install(){

  logW "\n--------软件为在线下载------------------开始部署JDK-----------------------------------"

  name=jdk
  logW "\n1. 传送脚本到远程主机" #1-20#
  logY "scp -P $sshport $name-install.sh public_functions $sshuser@$sship:/tmp/" #1-20#
    scp -P $sshport $name-install.sh public_functions $sshuser@$sship:/tmp/
  checkTF #21#
  
  logW "2. 修改远程脚本文件为可执行" #1-20#
  logY "ssh -p $sshport $sshuser@$sship 'chmod +x /tmp/$name-install.sh /tmp/public_functions'" #1-20#
    ssh -p $sshport $sshuser@$sship 'chmod +x /tmp/'$name'-install.sh /tmp/public_functions'
  checkTF #21#
  
  logW "3. 开始远程执行脚本文件" #1-20#
  logY "ssh -p $sshport $sshuser@$sship '/tmp/$name-install.sh'" #1-20#
    logG "---------------------远程主机开始安装---------------------\n"
    ssh -p $sshport $sshuser@$sship 'bash /tmp/'$name'-install.sh'
}

##7 安装nacos到远程主机 (可变模块)
nacos_Install(){

  logW "\n--------软件为本地查找------------------开始部署NACOS-----------------------------------" && echo

  readonly nacos_Package="nacos-server-2.1.1.tar.gz"    # nacos压缩包名称
  
  logW "开始在本机查找 $nacos_Package" #1-20#
  logY "find / -name $nacos_Package" #1-20#
    find / -path /proc -prune -o -name $nacos_Package | grep -v '/proc' #查找并显示文件路径
    find / -path /proc -prune -o -name $nacos_Package | grep -Eo --silent "$nacos_Package" #判断查找是否成功
    if [[ $? == 0 ]]; then
      nacos_url=$(find / -path /proc -prune -o -name $nacos_Package | grep -v '/proc') #把路径赋值给变量
      checkTF #21#
    else
      logR "软件未找到"
      exit
    fi
  
  name=nacos
  logW "\n1. 传送脚本和软件包到远程主机" #1-20#
  logY "scp -P $sshport $name-install.sh public_functions $sshuser@$sship:/tmp/" #1-20#
    scp -P $sshport $name-install.sh public_functions $sshuser@$sship:/tmp/
  checkTF #21#
  logY "scp -P $sshport $nacos_url $sshuser@$sship:/tmp/" #1-20#
    scp -P $sshport $nacos_url $sshuser@$sship:/tmp/
  checkTF #21#
  
  logW "2. 修改远程脚本文件为可执行" #1-20#
  logY "ssh -p $sshport $sshuser@$sship 'chmod +x /tmp/$name-install.sh /tmp/public_functions'" #1-20#
    ssh -p $sshport $sshuser@$sship 'chmod +x /tmp/'$name'-install.sh /tmp/public_functions'
  checkTF #21#
  
  logW "3. 开始远程执行脚本文件" #1-20#
  logY "ssh -p $sshport $sshuser@$sship '/tmp/$name-install.sh'" #1-20#
    logG "---------------------远程主机开始安装---------------------\n"
    ssh -p $sshport $sshuser@$sship 'bash /tmp/'$name'-install.sh'
}


##8 安装elk到远程主机 (可变模块)
elk_Install(){

  logW "\n--------软件为本地查找------------------开始部署ELK-----------------------------------" && echo

  readonly e_Package="elasticsearch-7.9.3-linux-x86_64.tar.gz"    # elasticsearch压缩包名称
  readonly k_Package="kibana-7.9.3-linux-x86_64.tar.gz"    # kibana压缩包名称
  readonly l_Package="logstash-7.9.3.tar.gz"    # logstash压缩包名称
 
  for i in "$e_Package" "$k_Package" "$l_Package" 
  do
    logW "开始在本机查找 $i" #1-20#
    logY "find / -name $i" #1-20#
         find / -path /proc -prune -o -name $i | grep -v '/proc' #查找并显示文件路径
         find / -path /proc -prune -o -name $i | grep -Eo --silent "$i" #判断查找是否成功
         if [[ $? == 0 ]]; then
            url=$(find / -path /proc -prune -o -name $i | grep -v '/proc') #把路径赋值给变量
                logW "\n---- 传送 $i 到远程主机" #1-20#
                logY "scp -P $sshport $url $sshuser@$sship:/tmp/" #1-20#
                     scp -P $sshport $url $sshuser@$sship:/tmp/
                     checkTF #21#
         else
            logR "软件未找到"
            exit
         fi
  done
 
  name=elk #脚本名称变化的部分
  logW "\n1. 传送脚本到远程主机" #1-20#
  logY "scp -P $sshport $name-install.sh public_functions $sshuser@$sship:/tmp/" #1-20#
    scp -P $sshport $name-install.sh public_functions $sshuser@$sship:/tmp/
  checkTF #21#
  
  logW "2. 修改远程脚本文件为可执行" #1-20#
  logY "ssh -p $sshport $sshuser@$sship 'chmod +x /tmp/$name-install.sh /tmp/public_functions'" #1-20#
    ssh -p $sshport $sshuser@$sship 'chmod +x /tmp/'$name'-install.sh /tmp/public_functions'
  checkTF #21#
 
  logW "3. 开始远程执行脚本文件" #1-20#
  logY "ssh -p $sshport $sshuser@$sship '/tmp/$name-install.sh'" #1-20#
    logG "---------------------远程主机开始安装---------------------\n"
    ssh -p $sshport $sshuser@$sship 'bash /tmp/'$name'-install.sh'
}


##9 安装mysql到远程主机 (可变模块)
mysql_Install(){

  logW "\n--------软件为在线下载------------------开始部署MYSQL-----------------------------------"

  name=mysql
  logW "\n1. 传送脚本到远程主机" #1-20#
  logY "scp -P $sshport $name-install.sh public_functions $sshuser@$sship:/tmp/" #1-20#
    scp -P $sshport $name-install.sh public_functions $sshuser@$sship:/tmp/
  checkTF #21#
  
  logW "2. 修改远程脚本文件为可执行" #1-20#
  logY "ssh -p $sshport $sshuser@$sship 'chmod +x /tmp/$name-install.sh /tmp/public_functions'" #1-20#
    ssh -p $sshport $sshuser@$sship 'chmod +x /tmp/'$name'-install.sh /tmp/public_functions'
  checkTF #21#
  
  logW "3. 开始远程执行脚本文件" #1-20#
  logY "ssh -p $sshport $sshuser@$sship '/tmp/$name-install.sh'" #1-20#
    logG "---------------------远程主机开始安装---------------------\n"
    ssh -p $sshport $sshuser@$sship 'bash -x /tmp/'$name'-install.sh'

}

##10 安装redis到远程主机 (可变模块)
redis_Install() {

  logW "\n--------软件为在线下载------------------开始部署REDIS-----------------------------------"

  name=redis
  logW "\n1. 传送脚本到远程主机" #1-20#
  logY "scp -P $sshport $name-install.sh public_functions $sshuser@$sship:/tmp/" #1-20#
    scp -P $sshport $name-install.sh public_functions $sshuser@$sship:/tmp/
  checkTF #21#
  
  logW "2. 修改远程脚本文件为可执行" #1-20#
  logY "ssh -p $sshport $sshuser@$sship 'chmod +x /tmp/$name-install.sh /tmp/public_functions'" #1-20#
    ssh -p $sshport $sshuser@$sship 'chmod +x /tmp/'$name'-install.sh /tmp/public_functions'
  checkTF #21#
  
  logW "3. 开始远程执行脚本文件" #1-20#
  logY "ssh -p $sshport $sshuser@$sship '/tmp/$name-install.sh'" #1-20#
    logG "---------------------远程主机开始安装---------------------\n"
    ssh -p $sshport $sshuser@$sship 'bash /tmp/'$name'-install.sh'

}

##11 安装emqx到远程主机 (可变模块)
emqx_Install() {

  logW "\n--------软件为在线下载------------------开始部署emqx-----------------------------------"

  name=emqx
  logW "\n1. 传送脚本到远程主机" #1-20#
  logY "scp -P $sshport $name-install.sh public_functions $sshuser@$sship:/tmp/" #1-20#
    scp -P $sshport $name-install.sh public_functions $sshuser@$sship:/tmp/
  checkTF #21#
  
  logW "2. 修改远程脚本文件为可执行" #1-20#
  logY "ssh -p $sshport $sshuser@$sship 'chmod +x /tmp/$name-install.sh /tmp/public_functions'" #1-20#
    ssh -p $sshport $sshuser@$sship 'chmod +x /tmp/'$name'-install.sh /tmp/public_functions'
  checkTF #21#
  
  logW "3. 开始远程执行脚本文件" #1-20#
  logY "ssh -p $sshport $sshuser@$sship '/tmp/$name-install.sh'" #1-20#
    logG "---------------------远程主机开始安装---------------------\n"
    ssh -p $sshport $sshuser@$sship 'bash /tmp/'$name'-install.sh'

}

##12 安装minio到远程主机 (可变模块)
minio_Install() {

  logW "\n--------软件为在线下载------------------开始部署minio-----------------------------------"

  name=minio
  logW "\n1. 传送脚本到远程主机" #1-20#
  logY "scp -P $sshport $name-install.sh public_functions $sshuser@$sship:/tmp/" #1-20#
    scp -P $sshport $name-install.sh public_functions $sshuser@$sship:/tmp/
  checkTF #21#
  
  logW "2. 修改远程脚本文件为可执行" #1-20#
  logY "ssh -p $sshport $sshuser@$sship 'chmod +x /tmp/$name-install.sh /tmp/public_functions'" #1-20#
    ssh -p $sshport $sshuser@$sship 'chmod +x /tmp/'$name'-install.sh /tmp/public_functions'
  checkTF #21#
  
  logW "3. 开始远程执行脚本文件" #1-20#
  logY "ssh -p $sshport $sshuser@$sship '/tmp/$name-install.sh'" #1-20#
    logG "---------------------远程主机开始安装---------------------\n"
    ssh -p $sshport $sshuser@$sship 'bash /tmp/'$name'-install.sh'

}

##menu 菜单
print_Start_log() {

  systemVersionname=$(cat /etc/redhat-release)

  echo -e "
——————————————————————————————————————————————————————————————————————————————————————————————————————————
                  欢迎使用Linux工具箱 此系统版本：$systemVersionname
——————————————————————————————————————————————————————————————————————————————————————————————————————————
      远程部署菜单
—————————————————————
\e[1;33m
           A) 生成本机公钥和私钥 \e[32mok\e[33m                      1) 安装mysql \e[32mok\e[33m
	   B) 发送公钥到远程主机 \e[32mok\e[33m                      2) 安装redis \e[32mok\e[33m
	                                                 3) 安装jdk \e[32mok\e[33m
	   C) 部署 数据库服务器(mysql,redis)\e[32mok\e[33m           4) 安装nginx \e[32mok\e[33m
           D) 部署 应用服务器(jdk) \e[32mok\e[33m                    5) 安装emqx \e[32mok\e[33m
	   E) 部署 消息服务器(emqx,minio) \e[32mok\e[33m             6) 安装minio \e[32mok\e[33m
	   F) 部署 流媒体服务器(流媒体)                  7) 安装流媒体
	   G）部署 文件服务器(nginx,jdk,nacos,elk) \e[32mok\e[33m    8) 安装elk \e[32mok\e[33m
                                                         9）安装nacos \e[32mok\e[33m
	        										     \e[0m
——————————————————————————————————————————————————————————————————————————————————————————————————————————"
}

##start 脚本执行入口
shell_On_start() {

  print_Start_log ##7

  echo -n "请输入操作编号: "
  read menu_Num

  case $menu_Num in

    a|A)#生成本机公钥和私钥
      my_Ssh_keygen  ##1
      sleep 2
      bash menu.sh ;;

    b|B)#发送公钥到远程主机
      my_Ssh_copy_id  ##3
      sleep 2
      bash menu.sh ;;

    c|C)#数据库服务器
       input_User_ip_port ##3
       mysql_Install 
       redis_Install 
       print_Runtime;; #30#

    d|D)#部署应用服务器
       input_User_ip_port ##3
       jdk_Install ##6
       print_Runtime;; #30#

    e|E)#部署消息服务器
       input_User_ip_port ##3
       emqx_Install 
       minio_Install 
       print_Runtime;; #30#

    g|G)#部署文件服务器
       input_User_ip_port ##3
       nginx_Install && jdk_Install && nacos_Install && elk_Install;;  ##5 ##6 ##7 ##8

    1)#安装mysql
       input_User_ip_port ##3
       mysql_Install ##9
       print_Runtime;; #30#

    2)#安装redis
       input_User_ip_port ##3
       redis_Install ##9
       print_Runtime;; #30#

    3)#安装jdk
       input_User_ip_port ##3
       jdk_Install ##6
       print_Runtime;; #30#

    4)#安装nginx
       input_User_ip_port ##3
       nginx_Install ##5
       print_Runtime;; #30#

    5)#安装emqx
       input_User_ip_port ##3
       emqx_Install ##5
       ;; ##

    6)#安装minio
       input_User_ip_port ##3
       minio_Install
       ;;

    8)#安装elk
       input_User_ip_port ##3
       elk_Install ##8
       print_Runtime;; #30#
 
    9)#安装nacos
       input_User_ip_port ##3
       nacos_Install ##7
       print_Runtime;; #30#
    
    *)
      logR "编号输入错误!! 请重新输入!!" && sleep 2 && clear && shell_On_start ;;
  esac
}

shell_On_start ##8
